package com.onstar.cn.weight;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.ViewAnimator;

import com.onstar.cn.common.util.CommonUtil;
import com.onstar.cn.weight.anim.HomeLoadingLayout;

/**
 * Created by 6335yz on 2016/7/5.
 */
public class HomeBallView extends RelativeLayout {
    private View view;
    private ViewAnimator wrapper_va;
    private ImageView status_iv;
    private TextView status_tv;
    private HomeLoadingLayout login_loading_ll;
    private TextView login_tv;
    private TextView des_tv;
    private TextView re_failed_tv;
    private LinearLayout status_ll;
    private OnClickListener mListener;

    public HomeBallView(Context context) {
        super(context);
        init(context);
    }

    public HomeBallView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public HomeBallView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {
        view = LayoutInflater.from(context).inflate(R.layout.home_vechicle_status, null);
        addView(view);
        findView();
    }

    private void findView() {
        wrapper_va = (ViewAnimator) view.findViewById(R.id.wrapper_va);
        status_iv = (ImageView) view.findViewById(R.id.status_iv);
        status_tv = (TextView) view.findViewById(R.id.status_tv);
        login_tv = (TextView) view.findViewById(R.id.login_tv);
        re_failed_tv = (TextView) view.findViewById(R.id.re_failed_tv);
        des_tv = (TextView) view.findViewById(R.id.des_tv);
        login_loading_ll = (HomeLoadingLayout) view.findViewById(R.id.login_loading_ll);
        login_loading_ll.setVisibility(View.GONE);
        status_ll = (LinearLayout) view.findViewById(R.id.status_ll);
        showLoginLayout(getResources().getString(R.string.ball_login));
    }

    public void setLoginListener(OnClickListener listener) {
        login_tv.setOnClickListener(listener);
    }

    public void setStatusListener(OnClickListener listener) {
        if(login_loading_ll!=null) {
            login_loading_ll.stopAnim();
        }
        status_ll.setOnClickListener(listener);
        mListener = listener;
//        login_tv.setClickable(false);
    }

    public void showLoginLayout(String loginMsg) {
//        status_ll.setClickable(false);
        login_tv.setClickable(true);
        login_tv.setTextSize(22);
        login_loading_ll.setVisibility(View.GONE);
        wrapper_va.setDisplayedChild(0);
        login_tv.setText(loginMsg);
    }

    public void showStatusLayout(int res, String status, String statusMsg) {
        wrapper_va.setDisplayedChild(1);
//        status_ll.setClickable(true);
        des_tv.setText(statusMsg);
        if (CommonUtil.isNull(status)) {
//            status_tv.setVisibility(View.GONE);
            wrapper_va.setDisplayedChild(2);
            login_tv.setClickable(false);
            login_loading_ll.setVisibility(View.GONE);
            login_tv.setTextSize(13);
            login_tv.setText(statusMsg);
            re_failed_tv.setText(statusMsg);
            re_failed_tv.setOnClickListener(mListener);
        } else {
            status_tv.setVisibility(View.VISIBLE);
            if(status.contains("...") || status.length() > 2){
                status_iv.setVisibility(View.INVISIBLE);
                status_tv.setTextColor(Color.parseColor("#616263"));
                status_tv.setTextSize(22);
                status_tv.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
                status_tv.setText(status);
            }else {
                status_iv.setVisibility(View.VISIBLE);
                status_tv.setTextColor(Color.parseColor("#181A1C"));
                status_tv.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                status_tv.setTextSize(30);
                status_tv.setText(status);
            }
        }
        status_iv.setImageResource(res);
    }


    public void showAutoLogin() {
        wrapper_va.setDisplayedChild(0);
        login_tv.setClickable(false);
        login_tv.setTextSize(22);
        login_tv.setText(R.string.ball_home_login_ing);
        login_loading_ll.setVisibility(View.VISIBLE);
        login_loading_ll.startAnim();
    }
}
